﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using ADEVSO.Drawing.Graphics;

namespace ADEVSO.Drawing
{
    public class Image
    {
        public static byte[] ResizeImage(byte[] image, int width, int height)
        {
            return ResizeImage(image, width, height, GraphicsQuality.Medium);
        }

        public static byte[] ResizeImage(byte[] image, int width, int height, String quality)
        {
            GraphicsQuality _quality = GraphicsQuality.Medium;
            if (!String.IsNullOrEmpty(quality))
            {
                switch (quality.ToLower())
                {
                    case "low":
                        {
                            _quality = GraphicsQuality.Low;
                            break;
                        }
                    //case "medium":
                    //    {
                    //        _quality = GraphicsQuality.Medium;
                    //        break;
                    //    }
                    case "high":
                        {
                            _quality = GraphicsQuality.High;
                            break;
                        }
                }
            }
            return ResizeImage(image, width, height, _quality);
        }

        public static byte[] ResizeImage(byte[] image, int width, int height, GraphicsQuality quality)
        {
            using (System.Drawing.Image original_photo = System.Drawing.Image.FromStream(new MemoryStream(image)))
            {
                GraphicsSettings graphicsSettings = GraphicsSettings.New(quality);
                width = width > 0 ? width : original_photo.Width;
                height = height > 0 ? height : original_photo.Height;
                // Create a new blank canvas.  The resized image will be drawn on this canvas.
                using (Bitmap temp_photo = new Bitmap(width, height, graphicsSettings.PixelFormat))
                {
                    temp_photo.SetResolution(graphicsSettings.Resolution, graphicsSettings.Resolution);
                    using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(temp_photo))
                    {
                        graphics.CompositingMode = graphicsSettings.CompositingMode;
                        graphics.CompositingQuality = graphicsSettings.CompositingQuality;
                        graphics.InterpolationMode = graphicsSettings.InterpolationMode;
                        graphics.PixelOffsetMode = graphicsSettings.PixelOffsetMode;
                        graphics.SmoothingMode = graphicsSettings.SmoothingMode;
                        graphics.DrawImage(original_photo, new Rectangle(0, 0, width, height), 0, 0,
                                           original_photo.Width, original_photo.Height, GraphicsUnit.Pixel);
                    }
                    // Save out to memory and then to a file.  We dispose of all objects to make sure the files don't stay locked.
                    using (MemoryStream mm = new MemoryStream())
                    {
                        temp_photo.Save(mm, ImageFormat.Png);
                        image = mm.GetBuffer();
                    }
                }
            }
            return image;
        }

        /// <summary>
        /// Set new size
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="original_width"></param>
        /// <param name="original_height"></param>
        /// <returns>Return if size was changed</returns>
        public static bool NewSize(ref int width, ref int height, int original_width, int original_height)
        {
            bool resized = false;
            if (width > 0 && height > 0 && (width != original_width || height != original_height))
            {
                resized = true;
            }
            else if (width > 0 && width != original_width)
            {
                resized = true;
                height = original_height * width / original_width;
            }
            else if (height > 0 && height != original_height)
            {
                resized = true;
                width = original_width * height / original_height;
            }
            //if (original_height > original_width)
            //{
            //    height = targetSize;
            //    width = (original_width * targetSize / original_height);
            //}
            //else
            //{
            //    width = targetSize;
            //    height = (original_height * targetSize / original_width);
            //}
            return resized;
        }
    }
}
